import api from './api';
import { AxiosResponse } from 'axios';
import { UserRole } from '@/types';

// 家长类型定义
export interface Parent {
  id: number;
  username: string;
  name: string;
  email?: string;
  phone?: string;
  enabled: boolean;
  createTime: string;
  updateTime?: string;
  role: UserRole;
  avatar?: string | null;
}

// 分页响应接口
export interface PageResponse<T> {
  content: T[];
  pageable: {
    pageNumber: number;
    pageSize: number;
    sort: {
      empty: boolean;
      sorted: boolean;
      unsorted: boolean;
    };
    offset: number;
    paged: boolean;
    unpaged: boolean;
  };
  last: boolean;
  totalElements: number;
  totalPages: number;
  size: number;
  number: number;
  sort: {
    empty: boolean;
    sorted: boolean;
    unsorted: boolean;
  };
  numberOfElements: number;
  first: boolean;
  empty: boolean;
}

// 查询参数类型
export interface ParentQueryParams {
  page?: number;
  size?: number;
  name?: string;
  keyword?: string;
}

// 创建家长参数类型
export interface CreateParentParams {
  name: string;
  phone: string;
  email?: string;
  enabled: boolean;
}

// 更新家长参数类型
export interface UpdateParentParams {
  name?: string;
  phone?: string;
  email?: string;
  enabled?: boolean;
}

// 家长列表项类型
interface ParentListItem {
  id: number;
  name: string;
  phone: string;
  username: string;
}

// 家长列表响应类型
interface ParentListResponse {
  data: ParentListItem[];
  message: string;
  status: string;
}

/**
 * 获取家长列表
 * @param params 查询参数
 * @returns 家长列表分页数据
 */
export const getParents = async (params: ParentQueryParams = {}): Promise<PageResponse<Parent>> => {
  try {
    const { page = 0, size = 10, name, keyword } = params;
    const queryParams: Record<string, any> = { page, size };
    
    // 优先使用keyword进行搜索，如果没有keyword则使用name
    if (keyword) {
      queryParams.keyword = keyword;
    } else if (name) {
      queryParams.name = name;
    }
    
    console.log('家长查询参数:', queryParams);
    
    // 参考getAllParents函数的实现
    const response = await api.get('/admin/parents', {
      params: queryParams
    });
    
    console.log('获取家长列表数据:', response);
    
    // 直接返回响应数据
    return response;
  } catch (error) {
    console.error('获取家长列表失败:', error);
    throw error;
  }
};

/**
 * 获取所有家长列表
 * @returns 家长列表
 */
export const getAllParents = async (): Promise<ParentListItem[]> => {
  try {
    const response = await api.get<ParentListItem[]>('/admin/parents/list');
    console.log('获取所有家长列表数据:', response.data);
    return response.data;
  } catch (error) {
    console.error('获取所有家长列表失败:', error);
    throw error;
  }
};

/**
 * 创建家长
 * @param params 家长信息
 * @returns 创建的家长信息
 */
export const createParent = async (params: CreateParentParams): Promise<Parent> => {
  try {
    const response = await api.post<Parent>('/admin/parents', params);
    console.log('创建家长结果:', response.data);
    return response.data;
  } catch (error) {
    console.error('创建家长失败:', error);
    throw error;
  }
};

/**
 * 更新家长信息
 * @param id 家长ID
 * @param params 更新的家长信息
 * @returns 更新后的家长信息
 */
export const updateParent = async (id: number, params: UpdateParentParams): Promise<Parent> => {
  try {
    const response = await api.put<Parent>(`/admin/parents/${id}`, params);
    console.log('更新家长结果:', response.data);
    return response.data;
  } catch (error) {
    console.error('更新家长失败:', error);
    throw error;
  }
};

/**
 * 删除家长
 * @param id 家长ID
 * @returns 是否成功
 */
export const deleteParent = async (id: number): Promise<boolean> => {
  try {
    await api.delete(`/admin/parents/${id}`);
    console.log('删除家长成功');
    return true;
  } catch (error) {
    console.error('删除家长失败:', error);
    throw error;
  }
};

/**
 * 更新家长状态
 * @param id 家长ID
 * @param enabled 是否启用
 * @returns 更新后的家长信息
 */
export const updateParentStatus = async (id: number, enabled: boolean): Promise<Parent> => {
  try {
    const response = await api.put<Parent>(`/admin/parents/${id}`, { enabled });
    console.log('更新家长状态原始响应:', response.data);
    return response.data;
  } catch (error) {
    console.error('更新家长状态失败:', error);
    throw error;
  }
}; 